package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import keeto.com.vn.bms.model.InfoDoctorEntity;
import keeto.com.vn.bms.ui.activity.R;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.BaseAdapter;
import android.widget.EditText;

public class ListInfoDoctorAdapter extends BaseAdapter {
	private Context context;
	public static ArrayList<InfoDoctorEntity> listInfoDoctor;
	private LayoutInflater inflater;

	public ListInfoDoctorAdapter(Context context,
			ArrayList<InfoDoctorEntity> objects) {
		super();
		this.context = context;
		this.listInfoDoctor = objects;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.lst_static_doctor_adapter,
					parent, false);
			holder = new ViewHolder();
			holder.tvNameDoctor = (EditText) convertView
					.findViewById(R.id.adapter_name_doctor);
			holder.tvScheCount = (EditText) convertView
					.findViewById(R.id.adapter_count_schedule);
			holder.tvRevenue = (EditText) convertView
					.findViewById(R.id.adapter_revenue);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvNameDoctor.setText(listInfoDoctor.get(position)
				.getNameDoctor());
		holder.tvScheCount.setText(listInfoDoctor.get(position)
				.getCoutSchedule());
		holder.tvRevenue.setText(listInfoDoctor.get(position).getRevenue());
		holder.rowBot = (View) convertView.findViewById(R.id.rowBot);
		if (listInfoDoctor.get(position).getCoutSchedule().equals("")
				|| listInfoDoctor.get(position).getCoutSchedule().equals("0")) {
			holder.tvScheCount.setBackgroundResource(R.drawable.bg_popup_null);
			holder.tvScheCount.setHint(context.getResources().getString(
					R.string.import_count_schedule));
			holder.tvScheCount.setText("");
		}
		if (listInfoDoctor.get(position).getRevenue().equals("")
				|| listInfoDoctor.get(position).getRevenue().equals("0")) {
			holder.tvRevenue.setBackgroundResource(R.drawable.bg_popup_null);
			holder.tvRevenue.setHint(context.getResources().getString(
					R.string.import_count_revenue));
			// holder.tvScheCount.setText("");
		}
		holder.tvScheCount
				.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub

						holder.tvScheCount.setHint("");

					}
				});
		holder.tvScheCount.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (!s.toString().equals(null) && !s.toString().equals("")) {
					listInfoDoctor.get(position)
							.setCoutSchedule((s.toString()));
					Log.e("listInfoDoctor.get(position)",
							listInfoDoctor.get(position).getCoutSchedule() + "");
				}

			}
		});

		holder.tvRevenue.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub

				holder.tvRevenue.setHint("");

			}
		});
		holder.tvRevenue.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (!s.toString().equals(null) && !s.toString().equals("")) {
					listInfoDoctor.get(position).setRevenue(s.toString());
					Log.e("listInfoDoctor.get(position)",
							listInfoDoctor.get(position).getRevenue() + ".");
				}
			}
		});
		return convertView;
	}

	@Override
	public int getCount() {
		return listInfoDoctor.size();
	}

	@Override
	public InfoDoctorEntity getItem(int position) {
		// TODO Auto-generated method stub
		return getItem(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	class ViewHolder {
		EditText tvRevenue;
		EditText tvScheCount;
		EditText tvNameDoctor;
		View rowBot;

	}

}
